#!/bin/bash

# camp for fluxbox:
# Convert And Make Preview
# for styles to become usable with https://github.com/ohnonot/fluxStyle

me="$(basename "$0")"
menu="$HOME/.fluxbox/menu"
init="$HOME/.fluxbox/init"
overlay="$HOME/.fluxbox/overlay"
s="$(date +%s)"

app1="urxvt -geometry 20x3+5-70 -e dash"
app2="urxvt -geometry 20x3+20-35 -e dash"

offset="$(xwininfo -root|grep Height|cut -d: -f2)"
offset="$((offset - 200))"

counter=0
movecounter=0

usage() {
	(( $# > 0 )) && echo "$@"
	echo -e "\n  Arguments:
  \$1 = Source styledir
  \$2 = Destination styledir (will be created if necessary)
  Only styles that require adjustment will be copied over.\n
  Convert:      styles that are a single file will be moved to their
                own directory, and renamed theme.cfg
  Make Preview: The style is applied, some windows are opened
                and a preview is generated according to
                tenr.de/howto/style_fluxbox/style_fluxbox.html#rules
                Please make sure the bottom left 400x200px of your
                current desktop are uncluttered.\n
  Source directory is:  \"$source\"\n  Dest. directory is:   \"$dest\"\n"
	exit 1
}

############# USER OPTIONS ################
rcs="r" # if destination exists, (r)ename or (c)lobber - everything else means skip
movedir="$HOME/.fluxbox/unused_styles.$me.$s" # set to a writable directory, and it will move source styles there, if permitted
mkdir "$movedir" || usage 
# note: mkdir here without -p option!

source="$1"
dest="$2"

[ -w "$init" ] || usage "Write permissions are required for $init"

[[ $# != 2 ]] && usage "Wrong number of arguments: $@"

[ -r "$source" ] || usage "Cannot read from source directory $source"
[ -d "$source" ] || usage "$source is not a directory."
mkdir -p "$dest" || usage

menubackup="$menu.$me.$s"
cp "$menu" "$menubackup"
initbackup="$init.$me.$s"
cp "$init" "$initbackup"
overlaybackup="$overlay.$me.$s"
mv "$overlay" "$overlaybackup"

finish() {
	kill $appid1 $appid2 >/dev/null 2>&1
	mv "$initbackup" "$init"
	mv "$menubackup" "$menu"
	mv "$overlaybackup" "$overlay"
	rmdir "movedir" >/dev/null 2>&1
	kill -s USR1 "$(xprop -root _BLACKBOX_PID | cut -d= -f2)" >/dev/null 2>&1
}
trap finish EXIT

makepreview() {
	echo -e "session.styleFile:$1
session.*.iconbar.alignment:Left
session.*.iconbar.iconWidth:100
session.*.workspaceNames:one,two,three,four
session.*.toolbar.tools:clock,systemtray,prevworkspace,workspacename,nextworkspace,iconbar" > "$init"
	kill -s USR1 "$(xprop -root _BLACKBOX_PID | cut -d= -f2)"
	sleep 0.6
	$app1 & appid1=$!
	sleep 0.1
	xdotool mousemove 280 $((offset + 20)) click 3 click 1 key Up key Right
	sleep 0.1
	$app2 & appid2=$!
	sleep 0.1
	convert x:"root[400x200+0+$offset]" -quality 95 "$1/preview.jpg"
	kill $appid1 $appid2
	fbsetroot -solid grey5
	sleep 0.1
} >/dev/null 2>&1

move() {
	if [ -d "$movedir" ] && [ -w "$movedir" ] && [ -w "$1" ]; then
		mv -i "$1" "$movedir/"
		echo "Moved $1 to $movedir..."
		((movecounter++))
	fi
}

#########################################################################

xdotool key Ctrl+Alt+Right

echo "[begin] (Fluxbox 1.3.7)
  [exec] (Terminal) {my term}
[separator]
[submenu] (Submenu)
  [exec] (Browser)
[end]
[end]
" > "$menu"

for i in $source/*
do
	if [ -d "$i" ]; then
		if [ ! -r "$i/preview.jpg" ]; then
			echo "No preview for: $i - copying over."
			if [ ! -d "$dest/${i##*/}" ]; then
				cp -ri "$i" "$dest/${i##*/}"
				move "$i"
				((counter++))
				makepreview "$dest/${i##*/}"
			else
				case $rcs in
				r)
				cp -ri "$i" "$dest/${i##*/}.$s"
				move "$i"
				((counter++))
				makepreview "$dest/${i##*/}.$s"
				;;
				c)
				rm -r "$dest/${i##*/}"
				cp -ri "$i" "$dest/${i##*/}"
				move "$i"
				((counter++))
				makepreview "$dest/${i##*/}"
				;;
				esac
			fi
		else
			echo "                $i"
		fi
	elif [ -r "$i" ]; then
			echo "     Is a file: $i - converting incl. preview"
		if [ ! -d "$dest/${i##*/}" ]; then
			mkdir "$dest/${i##*/}"
			cp -i "$i" "$dest/${i##*/}/theme.cfg"
			move "$i"
			echo "     Is a file: $i - converting incl. preview"
			((counter++))
			makepreview "$dest/${i##*/}"
		else
			case $rcs in
			r)
			mkdir "$dest/${i##*/}.$s"
			cp -i "$i" "$dest/${i##*/}.$s/theme.cfg"
			move "$i"
			echo "     Is a file: $i - converting incl. preview"
			((counter++))
			makepreview "$dest/${i##*/}.$s"
			;;
			c)
			rm -r "$dest/${i##*/}"
			cp -i "$i" "$dest/${i##*/}/theme.cfg"
			move "$i"
			echo "     Is a file: $i - converting incl. preview"
			((counter++))
			makepreview "$dest/${i##*/}"
			;;
			esac
		fi
	fi
done

echo "Copied over $counter styles and created previews for them.
Moved $movecounter styles to $move."

exit 0
